<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\member\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use util\Tree;
use think\Db;

/**             
 * 子账户角色
 */
class Memberrule extends Admin
{


    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $data_list = Db::name('member_role')->order('roleid DESC')->select();

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('子账户角色')// 设置页面标题
            ->setTablename('member_role')// 设置数据表名
            ->setPrimaryKey('roleid')
            ->addColumns([ // 批量添加列
                ['rolename', '名称'],
                ['status', '状态', 'status'],
                ['right_button', '操作', 'btn']
            ])
            ->addRightButtons('edit')// 批量添加右侧按钮
            ->addTopButtons('add')
            ->addRightButton('delete',['icon' => 'fa fa-times', 'title' => '删除', 'href' => url('delete', ['id' => '__id__'])])
            ->setRowList($data_list)// 设置表格数据
            ->fetch(); // 渲染页面
    }

    //新增
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if(empty($data['rolename'])){
                $this->error('账户名称不能为空');
            }
            $res=Db::name('member_role')->insert($data);
            if ($res) {
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增子账户角色')// 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['text', 'rolename', '名称', '必填'],
                ['radio', 'status', '状态', '', ['禁用','启用'],'1']
            ])
            ->layout(['rolename' => '6', 'status' => '6'])
            ->fetch();
    }

    //修改
    public function edit($id = 0)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if(empty($data['rolename'])){
                $this->error('账户名称不能为空');
            }
            $res=Db::name('member_role')->update($data);
            if ($res) {
                return $this->success('修改成功', cookie('__forward__'));
            } else {
                return $this->error('修改失败');
            }
        }
        $info = db('member_role')->where('roleid', $id)->find();
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('修改子账户角色')// 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden', 'roleid'],
                ['text', 'rolename', '名称', '必填'],
                ['radio', 'status', '状态', '', ['禁用','启用'],'1']
            ])
            ->setFormData($info)
            ->layout(['rolename' => '6', 'status' => '6'])
            ->fetch();
    }

    //删除 硬删
    public function delete($id = 0){
        $result = db('member_role')->where('roleid', $id)->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}